<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\logic\Database;
use brand\page\Table;

class Radio extends Component
{

    public function _init()
    {
        $this->component = 'radio';
    }


    public function Table($this_column)
    {
        parent::Table($this_column);
        $show_type = $this->getTableParam('show_type') ?: '';
        if($show_type == 'curd' || $show_type == ''){
            $options = $this->issetTableValue('options');
            if ( $options){

                $this_column = $this_column->scopedSlots(
                    [
                        Table::$SurfaceTable->select()->props(
                            [
                                'async'   => ['method' => 'post', 'data' => [$this->pk], 'url' => Table::$update_url],
                                'options' => $this->config['table']['options'],
                            ]
                        ),
                    ]
                );
            }
        }



        return $this_column;

    }

    /**
     * table表格内容
     * @param $key
     * @param $value
     */
    public function checkTableValue($key, $value)
    {

        $value = parent::checkTableValue($key, $value);
        $show_type = $this->getTableParam('show_type') ?: '';

        if($show_type == 'options'){
            $value = $this->getTableParam('options')[$value] ?? "";
        }

        return $value;
    }


    /**
     * 创建Curd表单
     * @param $field
     * @param $props
     */
    public function DbToCurd($field,$props = [])
    {
        $column = \brand\Brand::Builder()->form_props($props)->search_condition('=');
        $comment = Database::curdComment($field['comment']);
        if($comment['params']){

            $column->form_method('radio')->form_options($comment['params'])
                ->table_action_type('radio')->table_options($comment['options'] ?? []);
        }
        return $column;
    }


    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {
        return $this->buildSelectConfig($tableConfigValue,'Radio',$curd_method);
    }

    public function methodFormConf($form,$table,$params)
    {
        $param = $params['param'] ?? [];
        $param = BrandHelper::optionsStrToInt($param);
        $form['options'] = $param;
        $table['action_type'] = 'radio';
        $table['options'] = BrandHelper::encodeOptions($param);

        return [$form,$table];
    }

}